package com.yaj.mesif.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaj.mesif.dto.ToCanOrderDto;
import com.yaj.mesif.entity.ToCanOrder;
import com.yaj.mesif.service.IFO1004Service;
import com.yaj.mesif.service.ToCanOrderService;
import com.yaj.mesif.service.ToOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class IFO1004ServiceImpl implements IFO1004Service {
    @Autowired
    private ToCanOrderService toCanOrderService;
    @Autowired
    private ToOrderService toOrderService;

    @Value("${u8.url_u8_momclose}")
    private String url_U8_MomClose;

    @Value("${u8.caccid}")
    private String CACCID;

    private static final String CMAKER = "IFO1004";

    @Override
    public void ifo1004() {
        try {
            List<ToCanOrder> toCanOrders = toCanOrderService.GetCanOrderList();
            log.info("ifo1004 toCanOrders size:"+ toCanOrders.size());
            for (ToCanOrder toCanOrder : toCanOrders
            ) {
                //定义一个转化对象
                ObjectMapper mapper = new ObjectMapper();
                String json = null;

                ToCanOrderDto toCanOrderDto = new ToCanOrderDto();
                //账套号
                toCanOrderDto.setCACCID(CACCID);
                //订单号
                toCanOrderDto.setCVOUCHCODE(toCanOrder.getOrderNo());
                //关闭日期
                toCanOrderDto.setDDATE(DateUtil.today());
                //关闭人
                toCanOrderDto.setCMAKER(CMAKER);

                try {
                    //json = mapper.writeValueAsString(toCanOrderDto);
                    json = new JSONObject(toCanOrderDto).toString();
                    log.info("ifo1004 toCanOrders json:"+ json);
                    String resp = HttpRequest.post(url_U8_MomClose)
                            .body(json)
                            .timeout(20000)
                            .execute().body();
                    log.info("ifo1004 request result:"+ resp);
                    JSONObject jsonObject = new JSONObject(resp);
                    //取得返回结果
                    String result = jsonObject.getStr("Result");
                    //失败原因
                    String description = jsonObject.getStr("Description");
                    //生成的U8单据号
                    String receiptNo = jsonObject.getStr("ReceiptNo");
                    //返回结果为True时
                    if (result.equals("T")) {
                        log.info("Cancel Order:{} Success", toCanOrder.getOrderNo());
                        //插入数据到ToOrder
                        toCanOrder.setCreatedBy("IFO1004");
                        toCanOrder.setCreateTimestamp(DateTime.now());
                        toCanOrder.setUpdatedBy("IFO1004");
                        toCanOrder.setUpdateTimestamp(DateTime.now());
                        toOrderService.ifo1004InsertToOrder(toCanOrder);
                        //更新ToCanOrder中的Process
                        toCanOrder.setProcessflag("8");
                        toCanOrderService.ifo1004UpdateToOrder(toCanOrder);
                    } else {
                        log.error("Cancel Order:{} Fail,Description: {},ReceiptNo: {}", toCanOrder.getOrderNo(), description, receiptNo);

                        toCanOrder.setUpdatedBy("IFO1004");
                        toCanOrder.setUpdateTimestamp(DateTime.now());
                        //更新ToCanOrder中的Process
                        toCanOrder.setProcessflag("9");
                        toCanOrderService.ifo1004UpdateToOrder(toCanOrder);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
